package com.dreamlive.cn.clog;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Process;
import android.text.TextUtils;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Thread;
import java.lang.reflect.Field;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/* loaded from: classes.dex */
public class CollectLog implements Thread.UncaughtExceptionHandler {
    private static CollectLog INSTANCE = new CollectLog();
    public static final String TAG = "com.dreamlive.cn.clog.CollectLog";
    private Context mContext;
    private Thread.UncaughtExceptionHandler mDefaultHandler;
    private Map<String, String> infos = new HashMap();
    private DateFormat formatter = new SimpleDateFormat("yyyyMMdd_HHmmss");
    private String filePath = "";

    private CollectLog() {
    }

    public static CollectLog getInstance() {
        return INSTANCE;
    }

    public void init(Context context) {
        this.mContext = context;
        this.mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    public void init(Context context, String str) {
        init(context);
        this.filePath = str;
    }

    @Override // java.lang.Thread.UncaughtExceptionHandler
    public void uncaughtException(Thread thread, Throwable th) {
        Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
        if (!handleException(th) && (uncaughtExceptionHandler = this.mDefaultHandler) != null) {
            uncaughtExceptionHandler.uncaughtException(thread, th);
            return;
        }
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException unused) {
        }
        Process.killProcess(Process.myPid());
        System.exit(1);
    }

    private boolean handleException(Throwable th) {
        if (th == null) {
            return false;
        }
        collectDeviceInfo(this.mContext);
        Log.e(TAG, saveCrashInfo2File(th));
        return false;
    }

    public void collectDeviceInfo(Context context) {
        Field[] declaredFields;
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 1);
            if (packageInfo != null) {
                this.infos.put("versionName", packageInfo.versionName == null ? "null" : packageInfo.versionName);
                this.infos.put("versionCode", packageInfo.versionCode + "");
            }
        } catch (PackageManager.NameNotFoundException unused) {
        }
        for (Field field : Build.class.getDeclaredFields()) {
            try {
                field.setAccessible(true);
                this.infos.put(field.getName(), field.get(null).toString());
            } catch (Exception unused2) {
            }
        }
    }

    private String saveCrashInfo2File(Throwable th) {
        File file;
        StringBuffer stringBuffer = new StringBuffer();
        for (Map.Entry<String, String> entry : this.infos.entrySet()) {
            stringBuffer.append("[" + entry.getKey() + ", " + entry.getValue() + "]\n");
        }
        stringBuffer.append("\n" + getStackTraceString(th));
        try {
            String str = "CRS_" + this.formatter.format(new Date()) + ".txt";
            File absoluteFile = this.mContext.getExternalFilesDir("logs").getAbsoluteFile();
            if (!TextUtils.isEmpty(this.filePath)) {
                File file2 = new File(this.filePath);
                if (!file2.exists()) {
                    file2.mkdirs();
                }
                file = new File(this.filePath + File.separator + str);
            } else {
                file = new File(absoluteFile + File.separator + str);
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(stringBuffer.toString().getBytes());
            fileOutputStream.close();
            return str;
        } catch (Exception unused) {
            return null;
        }
    }

    public static String getStackTraceString(Throwable th) {
        if (th == null) {
            return "";
        }
        for (Throwable th2 = th; th2 != null; th2 = th2.getCause()) {
            try {
                if (th2 instanceof UnknownHostException) {
                    return "";
                }
            } catch (Exception unused) {
                return "";
            }
        }
        StringWriter stringWriter = new StringWriter();
        th.printStackTrace(new PrintWriter(stringWriter));
        return stringWriter.toString();
    }
}
